package com.nulldev.util.java.swing.fastEPane;

import java.awt.Image;
import java.awt.image.ImageObserver;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;

import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.Element;
import javax.swing.text.StyleConstants;
import javax.swing.text.View;
import javax.swing.text.html.CSS;
import javax.swing.text.html.HTML;
import javax.swing.text.html.ImageView;
import javax.swing.text.html.HTMLEditorKit.HTMLFactory;

import com.nulldev.util.image.ImageUtil;
import com.nulldev.util.java.DataSet;
import com.nulldev.util.java.DataTriplet;

public class FastViewFactory extends HTMLFactory {
	//private final HashMap<Element, ImageView> cache = new HashMap<Element, ImageView>();
	
//	private final HashMap<URL, DataTriplet<Image, ImageObserver, DataSet<Integer, Integer>>> imageCache = new HashMap<URL, DataTriplet<Image, ImageObserver, DataSet<Integer, Integer>>>();
	
    public View create(Element elem) {
        AttributeSet attrs = elem.getAttributes();
        Object elementName = attrs.getAttribute(AbstractDocument.ElementNameAttribute);
        Object o = (elementName != null) ? null : attrs.getAttribute(StyleConstants.NameAttribute);
        if (o instanceof HTML.Tag) {
            HTML.Tag kind = (HTML.Tag) o;
            if (kind == HTML.Tag.HTML) {
                return new FastBlockView(elem);
        	}else if (kind == HTML.Tag.IMG) {
        		final FastImageView vx = new FastImageView(elem);
//        		if (!imageCache.containsKey(vx.getImageURL())) {
//        			imageCache.put(
//        					vx.getImageURL(),
//        					new DataTriplet<Image, ImageObserver, DataSet<Integer,Integer>>(
//        							vx.getImage(), vx.getImageObserver(), new DataSet<Integer, Integer>(vx.getWidth(), vx.getHeight())));
////        					new DataSet<Image, ImageObserver>(vx.getImage(), vx.getImageObserver()),
////        					new DataSet<Integer, Integer>(vx.getWidth(), vx.getHeight()));
//        		}else {
//        			if (vx.getImage() != null) {
//        				final FastImageView vxc = new FastImageView(elem);
//        				final DataTriplet<Image, ImageObserver, DataSet<Integer, Integer>> c = imageCache.get(vx.getImageURL());
//        				final DataSet<Integer, Integer> res = c.getC();
//        				vxc.setImageObserver(c.getB());
//        				//System.out.println(res);
//        				vxc.setSize(res.getA(), res.getB());
//        				//vxc.setImage(ImageUtil.scaleDown(c.getA(), 2));
//        				return vxc;
//        			}
//        		}
        		return vx;
//        		if (cache.containsKey(elem)) {
//        			final ImageView c = cache.get(elem);
//        			final FastImageView v = new FastImageView(elem);
//        			if (c != null && c.getImage() != null) {
//        				v.setImage(c.getImage());
//        			}
//        			if (c == null) {
//        				cache.remove(elem);
//        			}
//        			v.setElement(elem);
//        			return v;
//        		}else {
//        			final FastImageView vx = new FastImageView(elem);
//        			cache.put(elem, vx);
//        			return vx;
//        		}
        		//final vx = new ImageView(elem);
        	}else if (kind == HTML.Tag.IMPLIED) {
                String ws = (String) elem.getAttributes().getAttribute(CSS.Attribute.WHITE_SPACE);
                if ((ws != null) && ws.equals("pre")) {
                    return super.create(elem);
                }
                return new HTMLParagraphView(elem);
            } else if ((kind == HTML.Tag.P) ||
                    (kind == HTML.Tag.H1) ||
                    (kind == HTML.Tag.H2) ||
                    (kind == HTML.Tag.H3) ||
                    (kind == HTML.Tag.H4) ||
                    (kind == HTML.Tag.H5) ||
                    (kind == HTML.Tag.H6) ||
                    (kind == HTML.Tag.DT)) {
                // paragraph

//                return new ParagraphView(elem);
                return new HTMLParagraphView(elem);
            }
        }
        return super.create(elem);
    }
}
